package org.stone.project.tx.handler.msgHandler;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.stone.project.tx.config.CtxHolderProxy;
import org.stone.project.tx.handle.MsgTypeHandler;
import org.stone.project.tx.pojo.dto.UserInfoDto;
import org.stone.project.tx.service.TokenService;
import org.stone.project.tx.sysEnum.MsgType;
import org.stone.project.tx.sysEnum.TxType;
import org.stone.project.tx.utils.ResultUtil;

@Component
@Slf4j
public class LogoutTypeHandlerImpl  implements MsgTypeHandler {
    @Autowired
    private TokenService tokenService;

    @Override
    public TxType getTxType() {
        return TxType.LOGOUT;
    }

    @Override
    public void postMsg(String token, JSONObject data, ChannelHandlerContext ctx) {
        UserInfoDto userInfoByToken = tokenService.getUserInfoByToken(token);
        if (ObjectUtil.isNull(userInfoByToken)){
            ctx.writeAndFlush(ResultUtil.fail(MsgType.LOGIN,"Token失效"));
            return;
        }
        CtxHolderProxy.unLinkRemoveExist(ctx);
        ctx.writeAndFlush(ResultUtil.ok(MsgType.LOGOUT,"退出成功"));
    }
}
